package com.hns.can.energy.web;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.service.CommonInitParamService;
import com.hns.can.common.web.BaseController;
import com.hns.can.energy.service.DeptMileageService;
import com.hns.can.energy.vo.NewEnergy;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.DateTool;

/**
 * 部门里程controller
 * @author:zhangy
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@RequestMapping("deptMileage")
@Controller
public class DeptMileageController extends BaseController{ 
	
	final String[] headerNames={"营运线路","所属机构","运营日期","运行时长","运行里程"};
    final String[] headerFields={"lineName","organName","days","rngOprtDuraTr","rngMile"};

	@Autowired
	private DeptMileageService deptMileageService;
	@Autowired
    private CommonInitParamService commonInitParamService;

	/**
	 * 部门里程报表
	 * @param vo
	 * @return
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("searchDeptMileage")
	@ResponseBody
    public Object searchDeptMileage(NewEnergy vo) throws Exception{
		if (vo.getOrgIds() == null || vo.getBeginTime() == null || vo.getEndTime() == null) {
            return null;
        }
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
    	Pagination page = deptMileageService.searchDeptMileage(vo);
    	GridPage grid = new GridPage(page);
    	return this.getReturnResult(grid);
    }

	/**
	 * 部门里程excel
	 * @param response
	 * @param vo
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("exportDeptMileageExcel")
    @SuppressWarnings("rawtypes")
	public void exportDeptMileageExcel(HttpServletResponse response, NewEnergy vo) throws Exception{
    	List list = getDeptMileage(vo);
    	ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, 
    			"excel", URLEncoder.encode("部门里程明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "部门里程");
    }
    
	/**
	 * 部门里程pdf
	 * @param response
	 * @param vo
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("exportDeptMileagePdf")
    @SuppressWarnings("rawtypes")
	public void exportDeptMileagePdf(HttpServletResponse response, NewEnergy vo) throws Exception{
    	List list = getDeptMileage(vo);
    	ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, 
    			"pdf", URLEncoder.encode("部门里程明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "部门里程");
    }
    
	/**
	 * 导出的部门里程数据
	 * @param vo
	 * @return
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
    @SuppressWarnings("rawtypes")
	private List getDeptMileage(NewEnergy vo) throws Exception{
    	if (vo.getOrgIds() == null || vo.getBeginTime() == null || vo.getEndTime() == null) {
            return null;
        }
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
    	List list = deptMileageService.getDeptMileage(vo);
    	return list;
    }
    
}